#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/2/4 9:46
# @Description: 一个使用网络上面免费的代理IP池
# @Thanks: https://github.com/jhao104/proxy_pool (提供了代理IP的使用方案)
# @Author : diyhome
# @Site : https://github.com/diyhome/
# @File : proxy.py
# @Software: PyCharm

import requests


class ProxyIP:
    '''
    @description: 初始化函数,默认使用@proxy_pool所提供的测试IP
    @param {type} 代理池http地址,默认
    @return: 
    '''
    now_proxy_ip = ""
    def __init__(self, proxy_url="http://118.24.52.95"):
        self.url = proxy_url
        self.now_proxy_ip = self.get_proxy_ip()

    '''
    @description: 获取代理IP
    @param {type} 
    @return: ip:port
    '''    
    def get_proxy_ip(self):
        return requests.get(self.url + '/get/').json().get("proxy")
    '''
    @description: 删除失效的代理
    @param {type} 代理ip:port
    @return: 
    '''    
    def delete_proxy_ip(self, proxy_ip):
        requests.delete(self.url + '/delete/?proxy={}'.format(proxy_ip))
        self.now_proxy_ip = self.get_proxy_ip()
    '''
    @description: 一个简单包装请求头和使用代理池的方法
    @param {type} 目标网站域名
    @return: html.text
    '''    
    def getHtml(self, url):
        re_try_count = 5
        while re_try_count > 0:
            # noinspection PyBroadException
            try:
                html = requests.get(url, proxies={"http": "http://{}".format(self.now_proxy_ip)}, headers={'User-Agent': 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19'})
                if html.text.startswith('{"error"'):
                    raise Exception("Server error!")
                return html.text
            except Exception:
                re_try_count -= 1
            self.delete_proxy_ip(self.now_proxy_ip)
            return None


if __name__ == '__main__':
    proxy = ProxyIP()
    ip = proxy.get_proxy_ip()    # ip = ip:port
    print(proxy.getHtml('www.baidu.com'))

